package com.google.code.sms.android.provider;

import com.google.code.sms.android.domain.IndexElement;
import com.google.code.sms.android.domain.MediaElement;
import com.google.code.sms.android.domain.SearchCritera;
import com.google.code.sms.android.domain.SearchResult;
import com.google.code.sms.android.service.MusicService;
import com.google.code.sms.android.service.MusicServiceFactory;

import android.app.SearchManager;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import com.google.code.sms.android.R;

/**
 * Provides search suggestions based on queries to the server.
 *
 * @author Sindre Mehus
 */
public class CustomSuggestionsProvider extends ContentProvider {

    private static final String RESOURCE_PREFIX = "android.resource://com.google.code.sms.android/";
    private static final String[] COLUMNS = {"_id",
            SearchManager.SUGGEST_COLUMN_TEXT_1,
            SearchManager.SUGGEST_COLUMN_TEXT_2,
            SearchManager.SUGGEST_COLUMN_INTENT_DATA,
            SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA,
            SearchManager.SUGGEST_COLUMN_ICON_1};

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        String query = selectionArgs[0] + "*";
        SearchResult searchResult = search(query);
        return createCursor(searchResult);
    }

    private SearchResult search(String query) {
        MusicService musicService = MusicServiceFactory.getMusicService(getContext());
        if (musicService == null) {
            return null;
        }

        try {
            return musicService.search(new SearchCritera(query, 5, 10, 10), getContext(), null);
        } catch (Exception e) {
            return null;
        }
    }

    private Cursor createCursor(SearchResult searchResult) {
        MatrixCursor cursor = new MatrixCursor(COLUMNS);
        if (searchResult == null) {
            return cursor;
        }

        for (IndexElement artist : searchResult.getArtists()) {
            String icon = RESOURCE_PREFIX + R.drawable.ic_action_artist;
            cursor.addRow(new Object[]{artist.getId(), artist.getName(), null, artist.getId(), artist.getName(), icon});
        }
        for (MediaElement.Entry album : searchResult.getAlbums()) {
            String icon = RESOURCE_PREFIX + R.drawable.ic_action_album;
            cursor.addRow(new Object[]{album.getId(), album.getName(), album.getArtist(), album.getId(), album.getTitle(), icon});
        }
        for (MediaElement.Entry song : searchResult.getSongs()) {
            String icon = RESOURCE_PREFIX + R.drawable.ic_action_song;
            cursor.addRow(new Object[]{song.getId(), song.getTitle(), song.getArtist(), song.getParent(), null, icon});
        }
        return cursor;
    }

    @Override
    public boolean onCreate() {
        return false;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues contentValues) {
        return null;
    }

    @Override
    public int delete(Uri uri, String s, String[] strings) {
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
        return 0;
    }
}